#include "threadPool.h"

int pwdFunc(dirStack_t *dirstack, char *pwd_result, MYSQL *sql,
    const char *username) {
  MYSQL_RES *result;
  MYSQL_ROW row;
  char sql_query[256] = {0};
  snprintf(sql_query, sizeof(sql_query),
           "SELECT path FROM fileSystem WHERE id = %d AND username = '%s'",
           dirstack->top->data, username);
  if (0 != mysql_query(sql, sql_query)) {
    fprintf(stderr, "pwdFunc() query failed: %s\n", mysql_error(sql));
    return -1;
  }
  result = mysql_store_result(sql);
  if (NULL == result) {
    fprintf(stderr, "pwdFunc() no result\n");
    return -1;
  }
  if (NULL != (row = mysql_fetch_row(result))) {
    snprintf(pwd_result, 256 * sizeof(char), "%s", row[0]);
    printf("pwdFunc() get path: \"%s\" successful\n", pwd_result);
  }
  mysql_free_result(result);
  return 0;
}
